package com.kamloo.stg.dialog;

import java.util.ArrayList;
import java.util.List;

import com.kamloo.stg.data.db.Direction;
import com.kamloo.stg.data.db.DirectionDAO;
import com.kamloo.stg.fragment.TabBusStop;
import com.kamloo.stg.info.AppConstant;
import com.kamloo.stg.R;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;


public class TabBusDirectionDialog extends DialogFragment
{	
	//----- GLOBAL VARIABLE -----
	private int m_lineId;
	private List<Direction> m_directions;
	
	
	//----- LISTENER -----
	OnClickListener radioButtonClickListener = new OnClickListener()
	{
		public void onClick(View view) 
		{		
		    boolean checked = ((RadioButton) view).isChecked();
		    FragmentManager manager = getActivity().getSupportFragmentManager();
		    
		    FragmentTransaction ft = manager.beginTransaction();
			// Create and replace the fragment that represent the tabs content
			Fragment newFragment = Fragment.instantiate(getActivity(), TabBusStop.class.getName(), null);			
			ft.replace(R.id.realtabcontent, newFragment, AppConstant.FRAGMENT_BUS_STOP);			
			
			// Back button and smooth transition feeling
			ft.addToBackStack(null);
			ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
			
			// Commit and execute
			ft.commit();
			manager.executePendingTransactions();    		
			
		    // Check which radio button was clicked
		    switch(view.getId()) 
		    {
		        case R.id.tabBusDialogDirectionA:
		            if (checked)
		            {
		                TabBusStop busStop = (TabBusStop)getActivity().getSupportFragmentManager().findFragmentByTag(AppConstant.FRAGMENT_BUS_STOP);
		                busStop.initializeView(m_directions.get(0).getId());		                
		            }
		            break;
		        case R.id.tabBusDialogDirectionB:
		            if (checked)
		            {
		            	TabBusStop busStop = (TabBusStop)getActivity().getSupportFragmentManager().findFragmentByTag(AppConstant.FRAGMENT_BUS_STOP);
		            	busStop.initializeView(m_directions.get(1).getId());
		            }
		            break;
		    }
		    
		    FragmentTransaction transaction = manager.beginTransaction();
		    transaction.remove(getActivity().getSupportFragmentManager().findFragmentByTag(AppConstant.FRAGMENT_TAB_BUS_DIRECTION_DIALOG));
		    transaction.commit();
		    manager.executePendingTransactions();		    
		}
	};
	
	
	//----- CLASS CODE -----
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
	{	
		View view = (LinearLayout)inflater.inflate(R.layout.tab_bus_direction_dialog, container, false);
		m_directions = getDirections(m_lineId);
		getDialog().setTitle(R.string.tabBus_DirectionDialogTitle);
		
		RadioButton directionA = (RadioButton) view.findViewById(R.id.tabBusDialogDirectionA);
		directionA.setOnClickListener(radioButtonClickListener);
		directionA.setText(m_directions.get(0).getIdentifier());
		
		if (m_directions.size() > 1) {
			RadioButton directionB = (RadioButton) view.findViewById(R.id.tabBusDialogDirectionB);
			directionB.setVisibility(View.VISIBLE);
			directionB.setOnClickListener(radioButtonClickListener);
			directionB.setText(m_directions.get(1).getIdentifier());			
		}	
		
		return view;
	}
	
    public void initializeView(int lineId)
    {
    	this.m_lineId = lineId;
    }
    
    private List<Direction> getDirections(int lineId)
    {
    	DirectionDAO directionDao = new DirectionDAO(getActivity());
    	directionDao.open();
    	
    	ArrayList<Direction> directions = directionDao.selectFromLine(lineId);    	
    	directionDao.close();
    	
    	return directions;
    }    
}
